//按钮点击事件
import { ajax, toast, print, cardCache } from 'nc-lightapp-front';
import linkBudgetApi from '../../../../../public/components/linkbudget/linkbudget';
import linkVoucherApi from '../../../../../public/components/linkvoucher/linkvoucher';
import linkQueryApi from '../../../../../public/components/linkquery/linkquerybills';
import config from '../config';
import _ from 'lodash-es';

export default function (props, key) {
    const checkedRows = props.table.getCheckedRows(config.bodycode);
    if (checkedRows.length !== 1) {
        toast({
            color: 'warning',
            content: this.state.multiLang['select_one_row']
        });
        return;
    }
    const record = _.get(checkedRows, '[0].data.values', {});
    const billId = _.get(record, 'pk_jkbx.value', '');
    const tradeType = _.get(record, 'djlxbm.value', '');
    const billType = _.get(record, 'pk_billtype.value', '');

    switch (key) {
        // 联查申请单
        case 'link_sqd':
            if (tradeType.startsWith('264a')) {
            } else {
                ajax({
                    url: '/nccloud/erm/loanmanage/LinkFysqAction.do',
                    data: {
                        tradetype: tradeType, //交易类型
                        openBillId: billId //单据主键ok
                    },
                    success: (result) => {
                        if (result.data && result.data.jkdBillModel) {
                            this.setState({
                                isLinkQueryFysqModelShowed: true,
                                linkQueryFysqModelSourceData: {
                                    linkData: result,
                                    tradetype: tradeType,
                                    openBillId: this.state.billId
                                }
                            });
                        } else if (result.data && result.data.sqdList) {
                            result.data.sqdList.forEach((item) => {
                                linkQueryApi.link({
                                    data: {
                                        openbillid: item.pk,
                                        tradetype: item.tradetype,
                                        props: this.props
                                    }
                                });
                            });
                        } else if (result.data && result.data.tradetype && result.data.pk) {
                            linkQueryApi.link({
                                data: {
                                    openbillid: result.data.pk,
                                    tradetype: result.data.tradetype,
                                    props: this.props
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            }
            break;

        // 联查预提单
        case 'link_ytd':
            if (tradeType.startsWith('264a')) {
            } else {
                ajax({
                    url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                    data: {
                        queryType: 'fyyt',
                        tradetype: tradeType, //交易类型
                        openBillId: billId //单据主键ok
                    },
                    success: (result) => {
                        if (result.data) {
                            this.setState({
                                isLinkQueryFyytModelShowed: true,
                                linkQueryFyytModelSourceData: {
                                    linkData: result,
                                    openBillId: billId,
                                    tradetype: tradeType
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            }
            break;
        // 联查借款单
        case 'link_jkd':
            // 借款单
            if (tradeType.startsWith('264a')) {
            } else if (tradeType.startsWith('264')) {
                ajax({
                    url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                    data: {
                        queryType: 'jkbx',
                        tradetype: tradeType, //交易类型
                        openBillId: billId //单据主键ok
                    },
                    success: (result) => {
                        if (result.data) {
                            this.setState({
                                isLinkQueryJkbxModelShowed: true,
                                linkQueryJkbxModelSourceData: {
                                    linkData: result,
                                    tradetype: tradeType,
                                    openBillId: billId
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            } else {
                ajax({
                    url: '/nccloud/erm/erbudgetmg/FysqLinkQueryAction.do',
                    data: {
                        userjson: JSON.stringify({ openbillid: billId, pk_tradetype: tradeType, tradetype: 'jk' })
                    },
                    success: (result) => {
                        if (result.data) {
                            this.setState({
                                isLinkQueryJkbxModelShowed: true,
                                linkQueryJkbxModelSourceData: {
                                    linkData: result,
                                    tradetype: tradeType,
                                    openBillId: billId
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            }
            break;
        // 联查报销单
        case 'link_bxd':
            // 借款单
            if (tradeType.startsWith('264a')) {
            } else if (tradeType.startsWith('263')) {
                ajax({
                    url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                    data: {
                        queryType: 'jkbx',
                        tradetype: tradeType, //交易类型
                        openBillId: billId //单据主键ok
                    },
                    success: (result) => {
                        if (result.data) {
                            this.setState({
                                isLinkQueryJkbxModelShowed: true,
                                linkQueryJkbxModelSourceData: {
                                    linkData: result,
                                    tradetype: tradeType,
                                    openBillId: billId
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            } else {
                ajax({
                    url: '/nccloud/erm/erbudgetmg/FysqLinkQueryAction.do',
                    data: {
                        userjson: JSON.stringify({ openbillid: billId, pk_tradetype: tradeType, tradetype: 'bx' })
                    },
                    success: (result) => {
                        if (result.data) {
                            this.setState({
                                isLinkQueryJkbxModelShowed: true,
                                linkQueryJkbxModelSourceData: {
                                    linkData: result,
                                    tradetype: tradeType,
                                    openBillId: billId
                                }
                            });
                        } else {
                            toast({
                                color: 'warning',
                                content: this.state.multiLang['query_link_none']
                            }); /* 国际化处理： 未联查到相关数据!*/
                        }
                    }
                });
            }
            break;

        // 联查审批
        case 'link_sp':
            this.setState({
                isApproveDetailShowed: true,
                approveDetailSourceData: {
                    billid: billId,
                    billtype: tradeType
                }
            });
            break;

        // 联查往来单据
        case 'link_wldj':
            // TODO 缺接口没有billdata数据
            // let billdata = props.createExtCardData(props.meta.getMeta().code, 'head', bodyarr);
            ajax({
                url: '/nccloud/erm/erm/billmanage/BillLinkJsAction.do',
                data: {
                    openbillid: billId,
                    pk_billtype: billType
                },
                success: (data) => {
                    console.log(data);
                    props.specialOpenTo(data.data.ys.url, {
                        id: data.data.ys.id,
                        pagecode: data.data.ys.pagecode,
                        appcode: data.data.ys.appcode,
                        scene: data.data.ys.scene,
                        status: data.data.ys.status,
                        scene: 'lc'
                    });
                    props.specialOpenTo(data.data.yf.url, {
                        id: data.data.yf.id,
                        pagecode: data.data.yf.pagecode,
                        appcode: data.data.yf.appcode,
                        scene: data.data.yf.scene,
                        status: data.data.yf.status
                    });
                },
                error: (data) => {
                    toast({ content: data.message, color: 'danger' });
                }
            });
            break;

        // 联查预算
        case 'link_ys':
            linkBudgetApi.link({
                data: {
                    tradetype: tradeType,
                    openbillid: billId
                },
                success: (data) => {
                    this.setState({
                        isInspectionShowed: true,
                        inspectionSourceData: data
                    });
                }
            });
            break;

        // 联查凭证
        case 'link_pz':
            linkVoucherApi.link({
                data: {
                    props: props,
                    record: {
                        relationID: billId,
                        pk_billtype: billType
                    },
                    appid: props.getSearchParam('ar')
                }
            });
            break;

        // 打印
        case 'print':
            let url = '';
            if (billType.startsWith('261')) {
                url = '/nccloud/erm/erbudgetmg/FysqPrintValidaAction.do';
            } else if (billType.startsWith('262')) {
                url = '/nccloud/erm/erbudgetmg/AccruedPrintValidaAction.do';
            } else if (billType.startsWith('263') || billType.startsWith('264')) {
                url = '/nccloud/erm/expenseaccount/ErmPrintValidaAction.do';
            }
            ajax({
                url,
                data: {
                    billId: billId,
                    billType: billType
                },
                success: (res) => {
                    if (!_.get(res, 'data.canPrint', false)) {
                        toast({
                            color: 'error',
                            content: res.data.errMesg
                        });
                        return;
                    } else {
                        let nodekey = '';
                        let funcode = '';
                        if (tradeType.startsWith('2647')) {
                            nodekey = '2647_IWEB';
                            funcode = '201102HKSP';
                        } else if (tradeType.startsWith('263')) {
                            nodekey = '2631_IWEB';
                            funcode = '201102JKSP';
                        } else if (tradeType.startsWith('264a')) {
                            nodekey = '264a_WEB';
                            funcode = '201102TZSP';
                        } else if (tradeType.startsWith('261')) {
                            nodekey = '2611_IWEB';
                            funcode = '201102SQSP';
                        } else if (tradeType.startsWith('262')) {
                            nodekey = '2621_IWEB';
                            funcode = '201102YTSP';
                        } else if (tradeType.startsWith('264')) {
                            nodekey = '2641_IWEB';
                            funcode = '201102BXSP';
                        }
                        print(
                            'pdf', //支持两类: 'html'为模板打印, 'pdf'为pdf打印
                            '/nccloud/erm/pub/ErmPrintBillAction.do', //后台服务url
                            {
                                funcode, //功能节点编码，即模板编码
                                nodekey, //模板节点标识
                                billtype: tradeType, //单据类型
                                oids: [billId] // 功能节点的数据主键   oids含有多个元素(['1001A41000000000A9LR','1001A410000000009JDD'])时为批量打印,
                            }
                        );
                    }
                }
            });
            break;
        default:
            break;
    }
}
